<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter />
        <ActionBar />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        :multiple-selected="false"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
      >
        <el-table-column v-for="column in insideColumns" :key="column.key" :width="column.width" :label="column.title" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button v-if="row.status !== 'enable'" type="text" title="编辑" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button v-if="row.status !== 'enable'" type="text" title="删除" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
            <el-button v-if="row.status !== 'enable'" type="text" title="启动" size="mini" icon="el-icon-video-play" @click="toEnable(row.id, 'enable')" />
            <el-button v-if="row.status === 'enable'" type="text" title="停用" size="mini" icon="el-icon-video-pause" @click="toDisable(row.id, 'disable')" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <ViewDialog v-if="dialogViewVisible" custom-class="second-dialog-height" title="详情" :show.sync="dialogViewVisible" width="60%" @on-cancel="viewCancel">
      <ViewForm :id="editId" />
    </ViewDialog>
    <FormDialog v-if="dialogFormVisible" custom-class="second-dialog-height" title="编辑" class="fixed-dialog-height" :show.sync="dialogFormVisible" width="60%" @on-ok="ok" @on-cancel="cancel">
      <EditForm :id="editId" ref="editForm" />
    </FormDialog>
  </div>
</template>

<script>

import ViewDialog from '@neu/biz-core/components/ViewDialog'
import FormDialog from '@neu/biz-core/components/FormDialog'
import TableFilter from './components/tableFilter'
import ViewForm from './components/viewForm'
import EditForm from './components/editForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate } from '@neu/biz-core/utils/utils'
import {
  page,
  remove,
  changeStatus
} from '@neu/dcom-ui/api/maintainRule'

export default {
  name: 'MaintainRulePage',
  components: {
    TableFilter,
    FormDialog,
    EditForm,
    ActionBar,
    PageableTable,
    ViewDialog,
    ViewForm
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  data() {
    return {
      r: page,
      d: remove,
      insideColumns: [
        {
          title: '维护规则编码',
          key: 'code'
        },
        {
          title: '维护规则名称',
          key: 'name'
        },
        {
          title: '维护类别',
          key: 'modelName'
        },
        {
          title: '是否停止服务',
          key: 'suspendService',
          render: (h, params) => {
            const text = params.row.suspendService === true ? '是' : '否'
            return h('span', [
              h('el-tag', text)
            ])
          }
        },
        {
          title: '量化时间(小时)',
          key: 'quantifyingTime'
        },
        {
          title: '创建时间',
          key: 'createTime',
          render: (h, params) => { return h('span', formatDate(params.row.createTime)) }
        },
        {
          title: '修改时间',
          key: 'updateTime',
          render: (h, params) => { return h('span', formatDate(params.row.updateTime)) }
        },
        {
          title: '维护说明',
          key: 'description',
          render: (h, params) => { // 查看功能
            return h('span', [
              h('el-link', {
                class: 'font-12',
                attrs: { title: '详情' },
                props: { type: 'primary' },
                on: { click: () => { this.view(params.row.id) } }},
              '详情')
            ])
          }
        },
        // {
        //   title: '规则来源',
        //   key: 'source',
        //   render: (h, params) => {
        //     const text = params.row.source === 'newcreate' ? '新建' : '临时任务'
        //     return h('span', [
        //       h('el-tag', text)
        //     ])
        //   }
        // },
        {
          title: '状态',
          key: 'status',
          render: (h, params) => {
            const text = params.row.status === 'enable' ? '启用' : '停用'
            return h('span', [
              h('el-tag', text)
            ])
          }
        }
      ],
      dialogFormVisible: false,
      dialogViewVisible: false,
      dialogStatus: 'create',
      editId: -1
    }
  },
  methods: {
    view(id) {
      this.editId = id
      this.dialogViewVisible = true
    },
    edit(id) {
      this.editId = id
      this.dialogFormVisible = true
    },
    ok() {
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        this.refresh()
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
    },
    viewCancel() {
      this.editId = -1
      this.dialogViewVisible = false
    },
    toEnable(id, flag) {
      changeStatus(id, flag).then(res => {
        this.$message({
          type: 'success',
          message: '启用成功!'
        })
        this.fetch()
      })
    },
    toDisable(id, flag) {
      changeStatus(id, flag).then(res => {
        this.$message({
          type: 'success',
          message: '停用成功!'
        })
        this.fetch()
      })
    }
  }
}
</script>
